package com.ljf.admin.config;

import com.ljf.admin.POJO.SysUser;
import com.ljf.admin.service.SysUserService;
import com.ljf.framework.exception.LjfUserInfoIncorrectException;
import com.ljf.framework.password.PasswordEncoderManager;
import com.ljf.framework.result.JsonResultEnum;
import com.ljf.framework.security.LjfSecurityManager;
import com.ljf.weixin.WeiXinManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * 说明：安全配置，自定义用户密码校验方式
 *
 * @Auther: lijinfeng
 * @Date: 2022/12/12
 */
@Configuration
public class LjfAuthConfig {

    @Autowired
    private SysUserService userService;

    // LJF-Auth 定制化配置 配置用户名密码验证方式
    @Autowired
    public void setCheckUserHandler() {
        LjfSecurityManager.getConfig().setCheckUserHandler(((username, password)->{
            SysUser sysUser = userService.selectUserByUserName(username);
//            System.out.println(PasswordEncoderManager.encode(password));
            if(!PasswordEncoderManager.matches(password,sysUser.getPassword())){
                throw new LjfUserInfoIncorrectException(JsonResultEnum.USERNAME_OR_PASSWORD_ERROR,"密码错误");
            }
            return sysUser.getId();
        }));
    }


    // 微信 定制化配置 配置用户名openid查找进行认证
    @Autowired
    public void setCheckWeiXinUserHandler() {
        WeiXinManager.getWeiXinHandler().setCheckWeiXinUserHandler(((openid, phoneNum)->{
            return 1l;
        }));
    }
}
